package org.jeecg.modules.door.job;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import lombok.extern.slf4j.Slf4j;
import org.jeecg.modules.door.entity.LwgyDoorGrouppassword;
import org.jeecg.modules.door.entity.LwgyDoorPassword;
import org.jeecg.modules.door.entity.LwgyJointDoor;
import org.jeecg.modules.door.service.ILwgyDoorGrouppasswordService;
import org.jeecg.modules.door.service.ILwgyDoorPasswordService;
import org.jeecg.modules.door.service.ILwgyJointDoorService;
import org.quartz.Job;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * TODO
 *
 * @author sn
 * @date 2023-08-18 15:20
 *同步门锁密码定时器
 */
@Slf4j
public class SysnLockGroupPasswordJob implements Job {

    @Autowired
    private ILwgyDoorPasswordService lwgyDoorPasswordService;

    @Autowired
    private ILwgyDoorGrouppasswordService lwgyDoorGrouppasswordService;
    @Autowired
    private ILwgyJointDoorService lwgyJointDoorService;




    @Override
    public void execute(JobExecutionContext jobExecutionContext) throws JobExecutionException {
        //获取分组有效密码
        QueryWrapper<LwgyDoorGrouppassword> queryWrapperGroup = new  QueryWrapper<>();
        queryWrapperGroup.eq("status",'1');
        queryWrapperGroup.eq("sysn",'0');//1是同步0是不同步
        List<LwgyDoorGrouppassword> groupList = lwgyDoorGrouppasswordService.list(queryWrapperGroup);
        if(groupList.size()>0){
            //有效密码对应分组
            List aList = groupList.stream().map(LwgyDoorGrouppassword::getGroupId).collect(Collectors.toList());
            QueryWrapper<LwgyJointDoor> queryGroup = new  QueryWrapper<>();
            queryGroup.eq("status",'1');
            queryGroup.eq("sysn",'0');//1是同步0是不同步
            queryGroup.in("group_id",aList);
            //查询分组里对应的房间
            List<LwgyJointDoor> pasList = lwgyJointDoorService.list(queryGroup);
            List<LwgyDoorPassword> list = new ArrayList<>();
            for(LwgyDoorGrouppassword item1:groupList){
                for(LwgyJointDoor item2 :pasList){
                    if(item1.getGroupId().equals(item2.getGroupId()) ){
                        //对接第三方


                    }
                }
                item1.setSysn("1");

            }
            lwgyDoorGrouppasswordService.updateBatchById(groupList);
        }




    }
}
